import html2canvas from "html2canvas";
import JSPDF from "jspdf";

export function html2Blob(ele) {
  return new Promise((resolve, reject) => {
    html2canvas(ele, {
      useCORS: true,
      scale: 2,
      dpi: 192,
      backgroundColor: null,
      allowTaint: true,
      ignoreElements: (element) => {
        if (element.tagName.toLowerCase() === "iframe") {
          return element;
        }
        return false;
      },
    })
      .then(async (canvas) => {
        var leftHeight = canvas.height;
        var a4Width = 595.28;
        var a4Height = 831.89;
        var a4HeightRef = Math.floor((canvas.width / a4Width) * a4Height);
        var position = 0;
        var pageData = canvas.toDataURL("image/jpeg", 1.0);
        var pdf = new JSPDF("p", "pt", "a4"); //A4纸，纵向
        var canvas1 = document.createElement("canvas");
        var height;
        pdf.setDisplayMode("fullwidth", "continuous", "FullScreen");
        async function createImpl(canvas) {
          if (leftHeight > 0) {
            // index++;
            var checkCount = 0;
            if (leftHeight > a4HeightRef) {
              var i = position + a4HeightRef;
              for (i = position + a4HeightRef; i >= position; i--) {
                var isWrite = true;
                for (var j = 0; j < canvas.width; j++) {
                  var c = canvas.getContext("2d").getImageData(j, i, 1, 1).data;
                  if (c[0] != 0xff || c[1] != 0xff || c[2] != 0xff) {
                    isWrite = false;
                    break;
                  }
                }
                if (isWrite) {
                  checkCount++;
                  if (checkCount >= 10) {
                    break;
                  }
                } else {
                  checkCount = 0;
                }
              }
              height =
                Math.round(i - position) || Math.min(leftHeight, a4HeightRef);
              if (height <= 0) {
                height = a4HeightRef;
              }
            } else {
              height = leftHeight;
            }
            canvas1.width = canvas.width;
            canvas1.height = height;
            var ctx = canvas1.getContext("2d");
            ctx.drawImage(
              canvas,
              0,
              position,
              canvas.width,
              height,
              0,
              0,
              canvas.width,
              height
            );
            if (position != 0) {
              pdf.addPage();
            }
            pdf.addImage(
              canvas1.toDataURL("image/jpeg", 1.0),
              "JPEG",
              10,
              10,
              a4Width,
              (a4Width / canvas1.width) * height
            );
            leftHeight -= height;
            position += height;
            if (leftHeight > 0) {
              setTimeout(createImpl, 500, canvas);
            } else {
              const rawdata = pdf.output("blob");
              resolve(rawdata);
            }
          }
        }
        if (leftHeight < a4HeightRef) {
          pdf.addImage(
            pageData,
            "JPEG",
            0,
            0,
            a4Width,
            (a4Width / canvas.width) * leftHeight
          );
          const rawdata = pdf.output("blob");
          resolve(rawdata);
        } else {
          try {
            pdf.deletePage(0);
            setTimeout(createImpl, 500, canvas);
          } catch (err) {
            console.log(err);
          }
        }
      })
      .catch((err) => {
        reject(err);
      });
  });
}
